<?php
	# Class: Log
	# ----------------------------------------------
	# This class provides a tool to save logs for administrator purposes.
	
	# Notice: When $file is not supplied as arguments, then $file is set to default file. The
	# mechanism works to reduces the number of IO requests to minimum.
	
	# No direct access
	
	defined('LOGS_DIR') or die('invalid');
	
	require_once('Joob/Utils/File.php');
	
	class Joob_Utils_Log{
	
		# VARIABLES
		private static $file; 
		private static $dir=LOGS_DIR; 
		private static $logFile=LOGS_DEFAULT_FILE;
		
		
		# PUBLIC FUNCTIONS
		# -----------------------------------------
		
		# Class Constructor
		
		public static function init(){
			self::$dir=LOGS_DIR;
			self::$file=new Joob_Utils_File();
		}
		
		
		
		# Function: setDir($dir)
		# ------------------------------------------
		# Setting the directory for the log instance. The default dir is 
		# LOGS_DIR, changable in the config folder.
		public static function setDir($dir=''){
			if ($dir==''){
				self::$dir=LOGS_DIR;
			}
			else{
				self::$dir=$dir;
			}
		}		
		
		
		# Function: write()
		# ------------------------------------------
		# Write a general string to the log file. Base methods for other 
		# functional calls.
		public static function write($data,$logFile='',$mod='a'){
			if ($logFile=='') $logFile=LOGS_DEFAULT_FILE;
			self::select($logFile,$mod);
			self::$file->write($data);
		}
		
		
		# Function: cache
		# ------------------------------------------
		# Write the log to the specific log file.
		
		public static function cache(&$message,$logFile='',$mod='a'){
			$time=date('Y-m-d H:i:s');
			$data="[$time] {$message}\n";
			self::write($data,$logFile,$mod);
		}
		
		
		# Function: save(&$message)
		# ------------------------------------------
		# Alias function of cache().
		
		public static function save(&$message,$logFile='',$mod='a'){
			self::cache($message,$logFile,$mod);
		}
		
		
		# Function: dump(&$var,&$file)
		# -------------------------------------------
		# Dump a variable to a file.
		
		public static function dump(&$var,$logFile='',$mod='a'){
			$data=print_r($var,true);
			self::log($data,$logFile,$mod);
		}
		
		
		# Function: select()
		# ------------------------------------------
		# Update the accessing mod and file name.
		
		private static function select(&$logFile='',$mod='a'){
			$c=Joob_Utils_Word::initial($logFile,1);
			confirm(Joob_Utils_Validator::alphaNumStrict($c));
			if ($logFile=='') $logFile=LOGS_DEFAULT_FILE;			
			self::$logFile=$logFile;
			self::$file->select(self::$logFile,self::$dir,$mod);
		}
		
		
		# Function: update()
		# -----------------------------------------
		# Alias of select() function.
		
		private static function update($logFile='',$mod='a')
		{
			self::select($logFile,$mod);
		}
	}
	
	//FIXME: Nen de o bootstrap?
//	Joob_Utils_Log::init();
?>